using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entities;
using System.Configuration;

namespace DAL
{
   public  class tbMadealDAL
    {
       private SqlConnection cn = null;
        public tbMadealDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbMadealDAL(tbMadealDAL dao)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbMadeal madeal)
        {
            SqlCommand cm = new SqlCommand("tbMadeal_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@HinhThucTT", madeal .HinhThucTT);
            cm.Parameters.AddWithValue("@MaSP", madeal.MaSP);
            cm.ExecuteNonQuery();
        }
        public void Update(tbMadeal madeal)
        {
            SqlCommand cm = new SqlCommand("tbMadeal_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Madeal", madeal .Madeal);
            cm.Parameters.AddWithValue("@HinhThucTT", madeal .HinhThucTT);
            cm.Parameters.AddWithValue("@MaSP", madeal.MaSP);
            cm.ExecuteNonQuery();
        }
        public void Delete(int ID)
        {
            SqlCommand cm = new SqlCommand("tbMadeal_Detele", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Madeal", ID);
            cm.ExecuteNonQuery();

        }
       public tbMadealCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("tbMadeal_GET", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Where", where);
           cm.Parameters.AddWithValue("@PageIndex", pageIndex);
           cm.Parameters.AddWithValue("@PageSize", pageSize);
           cm.Parameters.AddWithValue("@OrderBy", orderBy);
           cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
           cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
           SqlDataReader dr = cm.ExecuteReader();
           tbMadealCollection madealCollection = new tbMadealCollection();
           while (dr.Read())
           {
               tbMadeal madeal = new tbMadeal();
              madeal .Madeal  = int.Parse(dr.GetValue(0).ToString());
              madeal.HinhThucTT = int.Parse(dr.GetValue(1).ToString());
              madeal.MaSP = int.Parse(dr.GetValue(2).ToString());
               madealCollection.Add(madeal);
           }
           dr.Close();
           totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
           return madealCollection;


       }

    }
}
